package monitorcon

import (
    "log"
    "monitor/models"   
    "github.com/astaxie/beego/orm"
    "time"
    "gopkg.in/redis.v4"  //redis
)




func Getstackinfo(hostid string) string{
	
	 
	 
	 var s string
	 s= "{\"odata.id\":\"/redfish/v1/rich/Switches/"
	 s= s+string(hostid)
	 s=s+"/stack/stackMemberInfos\","
	 s=s+"\"opticalInfo\": [{"
     s=s+"\"vendorName\": \"testname\","
     s=s+"\"vendorPN\": \"pn\"}"
     s=s+",{ \"firstName\":\"Anna\" , \"lastName\":\"Smith\"}]"
	 s=s+"}"
	 	  
	 
       
     return s
}


func Parsemap(hostuuid string ,m map[string]interface{},rootarr [10]string,index int,tablename string){
   //对于k,v 来说，取1到index-1 的节点作为其root节点，root[0]预放""
   for k, v := range m {
        
        switch value:=v.(type){
          case string: // v : string
              
              savedata(hostuuid,rootarr,index,k,value,tablename)// taleid:1 for stackinfo 
              
              //value:= v.(string)
          case map[string]interface{}: //v:map[vendorName:testname vendorPN:pn]
              rootarr[index]=k
              Parsemap(hostuuid,v.(map[string]interface{}),rootarr,index+1,tablename)
          case []interface{}: //map array:for example:map[vendorName:testname vendorPN:pn] map[firstName:Anna lastName:Smith]
               
               log.Println("map array")
               for _, v2 := range value {
		         switch  vv:= v2.(type) {
		           case map[string]interface{}:
		             log.Println(vv);
		             log.Printf("%s",k); //k as root 
		             rootarr[index]=k
			         Parsemap(hostuuid,vv,rootarr,index+1,tablename)
		         }
              }
              
        }
    }
        
}

//root arr 对应数据库的node1,node2,node3...
func savedata(hostuuid string,rootarr [10]string,index int,key string ,value string,tablename string){
    var i int 
    
    nodeinfo := new(models.Nodeinfo)
    //from 1,root[0]=""
    for  i = 1; i < index; i++ {
      log.Printf("%s\n", rootarr[i])
      if (i==1){
        nodeinfo.Node1 = rootarr[i]
      }else if (i==2){
        nodeinfo.Node2 = rootarr[i]
      }
             
    }
    
    if (i==1){
      nodeinfo.Node1=key
    }else if (i==2){
      nodeinfo.Node2=key
    }else if (i==3){
      nodeinfo.Node3=key
    }
    
    log.Println(nodeinfo)
    log.Printf("%s -> %s\n", key, value) 
    
    orm.Debug = true
    o := orm.NewOrm()
    o.Using("default")
    
    if (tablename=="stack"){ //stackinfo
	    skinfo := new(models.Stackinfo)
	    skinfo.Hostuuid = hostuuid
	    
	    skinfo.Node1 = nodeinfo.Node1
	    skinfo.Node2 = nodeinfo.Node2
	    skinfo.Node3 = nodeinfo.Node3
	    skinfo.Value = value
	    skinfo.Creationtime =time.Now()
	 
	    log.Println(o.Insert(skinfo))
	}
   
    
}


func Savehostinfotoredis(rs *redis.Client,hostuuid string , field string ,info string){
   
   rs.HSet(hostuuid, field, info)
   log.Printf("save %s info to redis for %s",field,hostuuid)   
}


func Gethostinfofromredis(rs *redis.Client,field string ,hostuuid string) string{

   info, err := rs.HGet(hostuuid, field).Result()//得到token ,如果token=-1,则是空闲的连接
      
   if err != nil {
      log.Printf("getting %s info from redis error",field)
      return "";
   }
   
   return info
      
}



 